package com.gitee.zhanyao.common.util;


import com.gitee.zhanyao.common.model.IdCardInfo;

import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 身份证工具类
 */
public class IdCardUtil {

    private IdCardUtil() {
    }

    /**
     * 根据身份证号提取信息
     *
     * @param idNumber 身份证号码
     * @return 身份证信息
     */
    public static IdCardInfo getIdCardInfo(@NotNull String idNumber) {
        IdCardInfo idCardInfo = new IdCardInfo();
        idCardInfo.setIdNumber(idNumber);
        idCardInfo.setProvinceCode(idNumber.substring(0, 2));
        idCardInfo.setCityCode(idNumber.substring(2, 4));
        idCardInfo.setCountyCode(idNumber.substring(4, 6));
        idCardInfo.setBirthday(LocalDate.parse(idNumber.substring(6, 14), DateTimeFormatter.BASIC_ISO_DATE));
        idCardInfo.setGender(Integer.parseInt(idNumber.substring(16, 17)) % 2);
        return idCardInfo;
    }

}
